const os = require('os')
const fs = require('fs')
const path = require('path')
const app = require('electron').app
const appPath = path.dirname(app.getPath('exe'))
const userDataPath = app.getPath('userData')
const appDataPath = app.getPath('appData')
const tempDataPath = app.getPath('temp')
const desktopPath = app.getPath('desktop')
const asar = require('asar')
const nodexlsx = require('node-xlsx')
const electronSettings = require('electron-settings')
const electronSettingsPack = require('../assets/js/electronSettingsPack')
const jstoolsNode = require('jstools-common/dist/jstools.node')
const parseCubeLUT = require('parse-cube-lut')
const moovFaststart = require('moov-faststart')
const mp4MoovMove = require('../assets/js/mp4-moov-move')
const utf7 = require('utf7')
const nodemailer = require('nodemailer')
const jsonwebtoken = require('jsonwebtoken')
const uuencode = require('uuencode')
const xxencode = require('xxencode')
const bubblebabbler = require('bubblebabbler')
const Vigenere = require('vigenere')
const gronsfeld = require("@haydenhigg/gronsfeld")
const whois = require('whois-json')
const ffmpeg = require('ffmpeg')
const CipherJS = require('cipherjs')
const Evilscan = require('evilscan')
const regedit = require('regedit')
const vbsPath = path.join(__dirname, '../vbs')
regedit.setExternalVBSLocation(vbsPath)
const udpServer = require('./server-udp')
const wsServer = require('./server-websocket')
const httpServer = require('./server-http')
const webrtcServer = require('./server-websocket-webrtc')
const nodeMediaServer = require('./server-node-media')
const express = require('express')

// 程序信息
let appInfo = {
    appName: 'myApp',
    env: process.env.NODE_ENV, // development | production
    version: process.env.VUE_APP_VERSION
}

// 系统信息
let systemInfo = {
    hostname: os.hostname(),
    platform: `${process.platform} ${process.arch} ${os.release()}`,
    // eslint-disable-next-line no-prototype-builtins
    arch: (process.arch === 'x64' || process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432')) ? '64' : '32',
    ips: ['127.0.0.1'],
    ip: '127.0.0.1',
    userInfo: os.userInfo()
}
systemInfo.ips = jstoolsNode.getIpAddress()
systemInfo.ip = systemInfo.ips[0] || '127.0.0.1'

// 第三方库资源（因为require和window.require的问题，一些库不能在渲染进程中被正确加载，故挂载在此）
let packages = {
    jstoolsNode,
    electronSettings,
    electronSettingsPack,
    asar,
    nodexlsx,
    parseCubeLUT,
    moovFaststart,
    mp4MoovMove,
    utf7,
    nodemailer,
    jsonwebtoken,
    uuencode,
    xxencode,
    bubblebabbler,
    Vigenere,
    gronsfeld,
    whois,
    ffmpeg,
    CipherJS,
    Evilscan,
    regedit,
    express,
}

// 服务
let services = {
    udpServer,
    wsServer,
    webrtcServer,
    httpServer,
    nodeMediaServer,
}

// 本地仓储目录
let workDirectory = {
    appPath, // 程序目录
    tempDataPath, // 临时目录
    userDataPath, // 用户目录
    appDataPath, // %APPDATA%
    desktopPath: path.join(desktopPath, appInfo.appName), // 桌面存储地址
    appsPath: path.join(userDataPath, 'x-apps'), // 本地安装包存储地址
    logsPath: path.join(userDataPath, 'x-logs') // 日志存储
}
for (let k in workDirectory) {
    if (!fs.existsSync(workDirectory[k])) {
        fs.mkdirSync(workDirectory[k], {recursive: true})
    }
}
module.exports = {
    ...appInfo,
    ...systemInfo,
    ...packages,
    ...services,
    ...workDirectory
}
